local Buff 	  = require("app.battleV3.ui.objs.Buff")
local BarStatus = require("app.battleV3.ui.objs.BarStatus")
local Head 	  = require("app.battleV3.ui.objs.Head")
local TargetBoss = MClass("TargetBoss")
--local EnemyName_Color = cc.c4b(255,1,1, 255)
local Utils = require("app.utils.Util")

--ctor 初始化UI init设置静态数据 setData设置动态数据
function TargetBoss:ctor()
    --self.root 默认为根节点
    --Leader

    local leaderSide = {}
    self.leaderPanel = self:findChild("Panel/Panel_leader")
    leaderSide.name = self.leaderPanel:findChild("name")
    leaderSide.num = self.leaderPanel:findChild("num")
    leaderSide.head = Head.new(self.leaderPanel:findChild("Panel_head"))
    leaderSide.buffView = self.leaderPanel:findChild("Panel_buff")
    leaderSide.bar  = BarStatus.new(self.leaderPanel)
    leaderSide.lockBtn = self.leaderPanel:findChild("Button_lock")
	leaderSide.lockBtn:setBright(true)
    leaderSide.signBtn = self.leaderPanel:findChild("Button_sign")
	leaderSide.signBtn:setBright(true)

    leaderSide._buffkeys = {}

    for i=1, BattleUIConst.BuffMax do
    	local key = "buff_"..i
		table.insert(leaderSide._buffkeys,key)
		leaderSide[key] = Buff.new(leaderSide.buffView:findChild(string.format("buff_%d",i)))
	end
    self.leaderSide = leaderSide



    --Team
    local teamSide = {}

    self.teamPanel = self:findChild("Panel/Panel_team")
    teamSide.name = self.teamPanel:findChild("name")
    teamSide.num = self.teamPanel:findChild("num")
    teamSide.head = Head.new(self.teamPanel:findChild("Panel_head"))
    teamSide.buffView = self.teamPanel:findChild("Panel_buff")
    teamSide.bar  = BarStatus.new(self.teamPanel)
    teamSide.lockBtn = self.teamPanel:findChild("Button_lock")
	teamSide.lockBtn:setBright(true)

    teamSide._buffkeys = {}

    for i=1, BattleUIConst.BuffMax do
    	local key = "buff_"..i
		table.insert(teamSide._buffkeys,key)
		teamSide[key] = Buff.new(teamSide.buffView:findChild(string.format("buff_%d",i)))
	end
    self.teamSide = teamSide

    self.leaderPanel:setVisible(false)
    self.nowSide = self.teamSide
	self:init()
end

function TargetBoss:setLock(bLock)
	self.bLock = bLock
	self.nowSide.lockBtn:setBright(not bLock)
end

function TargetBoss:init()
   	--self.name:setTextColor(EnemyName_Color)
	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BUFF_UPDATE,handler(self, TargetBoss.updateBuffs))
	local function clickCallBack(sender,eventType)
		if eventType == ccui.TouchEventType.began then
		elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then
			--已经锁了
			BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_CANCEL_AUTO)
			if self.bLock then
				BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.TARGT_LOCK,false)
				self:setLock(false)
			else
				BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.TARGT_LOCK,true)
				self:setLock(true)
			end
		end
	end

	self.teamSide.lockBtn:addTouchEventListener(clickCallBack)
	self.leaderSide.lockBtn:addTouchEventListener(clickCallBack)
	local function clickMarkCallBack(sender,eventType)
		if eventType == ccui.TouchEventType.began then
		elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then
			--已经锁了
			BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_CANCEL_AUTO)
			if self.bMarked then
				BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.TARGT_TEAM_MARK, {targetId = self.targetId, flagId = 0})
				self:setMark(false)
			else
				BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.TARGT_TEAM_MARK, {targetId = self.targetId, flagId = 1})
				self:setMark(true)
			end
		end
	end

	self.leaderSide.signBtn:addTouchEventListener(clickMarkCallBack)
end

function TargetBoss:setMark(bMarked)
	self.bMarked = bMarked
	if self.nowSide.signBtn then
		self.nowSide.signBtn:setBright(not bMarked)
	end
end

function TargetBoss:setAsLeader(bLeader)
	if bLeader then
		self.leaderPanel:setVisible(true)
		self.teamPanel:setVisible(false)
		self.nowSide = self.leaderSide
	else
		self.leaderPanel:setVisible(false)
		self.teamPanel:setVisible(true)
		self.nowSide = self.teamSide
	end
end

function TargetBoss:reset()
	local keys = self.nowSide._buffkeys
	for i=1,8 do
		self.nowSide[keys[i]]:setTargetId(nil)
		self.nowSide[keys[i]]:setVisible(false)
	end
end

function TargetBoss:updateBuffs(data, targetId)
	if targetId~=self.targetId then return end
	self:reset()
	local keys = self.nowSide._buffkeys
	local i = 1
	-- for i=1,#data do
	for _, v in pairs(data) do
		if i <= BattleUIConst.BuffMax then
			self.nowSide[keys[i]]:setTargetId(self.targetId)
			self.nowSide[keys[i]]:setData(v)
			i = i + 1
		end
	end
end

function TargetBoss:setData(entityModel,view,targetId,bLeader)
	--if self.targetId == data.targetId then return end
	self:setVisible(true)
	self:setAsLeader(bLeader)
	self:setLock(false)
	self.nowSide.name:setString(string.format("%s lv%d",Utils.getStringInLen(entityModel:getName(),Constant.BATTLE_SHOW_NAME_MAX_LEN),entityModel:getLv()))
	self.targetId = targetId
	--self.icon:loadTexture(data.icon)

	self.nowSide.head:setData(view, entityModel)
	self.nowSide.bar:setData(view, entityModel)
	self:setMark(view:getMarkId() == 1)
	self.nowSide.head:setData(view, entityModel)
	self:reset()
	local buffList = view:getBuffIconDict()
	local keys = self.nowSide._buffkeys
	local i = 1
	-- for i=1, #buffList do
	for _, v in pairs(buffList) do
		if i > BattleUIConst.BuffMax then
			--todo
			break
		end
		self.nowSide[keys[i]]:setTargetId(self.targetId)
		self.nowSide[keys[i]]:setData(v)
		i = i + 1
	end
end

function TargetBoss:clearData()
	self:setLock(false)
	self.leaderSide.bar:clearHideEffect()
	self.teamSide.bar:clearHideEffect()
end

function TargetBoss:updateData(targetId,hpMin,hpMax,manaMin,manaMax,rageMin,rageMax,energyMin,energyMax,hpExtra,hpMaxExtra)
	if targetId ~= self.targetId then return end
	if hpMin <= 0 then
		self:setVisible(false)
	end
	self.nowSide.bar:updateData(hpMin,hpMax,manaMin,manaMax,rageMin,rageMax,energyMin,energyMax,hpExtra,hpMaxExtra)
end

function TargetBoss:shouldHideAllHpUI(flag)
	self.nowSide.bar:shouldHideAllHpUI(flag)
end

return TargetBoss
